package com.edu.edumanager.controller;

import com.alibaba.excel.EasyExcel;
import com.edu.edumanager.bean.EduSubject;
import com.edu.edumanager.bean.EduUser;
import com.edu.edumanager.service.ClassService;
import com.edu.edumanager.vo.EduResult;
import com.edu.edumanager.vo.EduSubjectList;
import com.edu.edumanager.vo.ElementTreeVo;
import com.edu.edumanager.vo.ExcelVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: edu-manager
 * @description:
 * @author: Jaime
 * @create: 2021-08-13 15:27
 **/
@RestController
@RequestMapping("/eduservice/subject")
@CrossOrigin
public class EduSubjectController {
    @Autowired
    ClassService classService;

    /**
     * 上传excel
     * @param file
     * @return
     */
    @RequestMapping("/excelUpload")
    public EduResult uploadExcel(MultipartFile file){

        EduResult eduResult = classService.uploadExcel(file);

        return eduResult ;
    };

    /**
     *  excel 下载
     * @param response
     * @param request
     */
    @RequestMapping("/excelDownload")
    public void downloadExcel(HttpServletResponse response, HttpServletRequest request) {
        try {
            //表示你要下载excel
            response.setContentType("application/vnd.ms-excel");
            //中文 utf-8
            response.setCharacterEncoding("utf-8");
            String fileName = URLEncoder.encode
                    ("模板文件", "UTF-8").
                    replaceAll("\\+", "%20");
            //真正的下载文件设置
            response.setHeader("Content-disposition",
                    "attachment;filename*=utf-8''" + fileName + ".xlsx");

             List<ExcelVo> eduSubjects = classService.selectAllSubject();
            //在这里 输出模板文件
            EasyExcel.write(response.getOutputStream(),
                    ExcelVo.class).sheet("模板")
                    .doWrite(eduSubjects);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/subjectAll")
    public EduResult subjectAll(@RequestParam(defaultValue = "0") Integer parentId) {
        System.out.println(parentId);
        List<ElementTreeVo> list = classService.findSubjectByParentId(parentId);
        System.out.println(list);
        return EduResult.ok().data("tree", list);

    }

    @RequestMapping("/deleteSubject/{id}")
    public EduResult deleteSubject(@PathVariable Integer id) {
        int count = classService.deleteSubject(id);
        return EduResult.ok().data("count", count);
    }

    @RequestMapping("/updateSubject/{id}/{value}")
    public EduResult updateSubject(@PathVariable Integer id,@PathVariable String value) {
        String label = classService.updateSubjectById(id,value);
        return EduResult.ok().data("label",label);
    }

    /**
     *
     * @return 添加页面返回的树结构
     */
    @RequestMapping("/getSubjectList")
    public EduResult getSubjectList(){
        List<EduSubjectList> subjectList = classService.getSubjectList();
        return EduResult.ok().data("subjectList",subjectList);
    }
}
